clear
set more off

global exper need4
global educ needbd_plus

global dto "change_openings_per_emp"
global initial "initialneed"
global level2000 "avgageexp"
global covars    "initialneed change_openings_per_emp avgageexp"
global stdcovars "stdinitialneed stdavgageexp stdchange_openings_per_emp"

global occfe "i.occsoc_num"
global stfe "i.statefip"
global timefe "b2012.year"

global styearfe "i.styear"
global occyearfe "i.occyear"

global restric "if minto>15 "

global flow "\parbox[c]{1.8cm}{\centering "
global lte "$\leq$"
global gte "$\geq$"

global weight "[w=occshare_state]"


use C:\Users\shoag\Dropbox\Upskilling_sharedfolder\revised_paper\data\BGT_2007_2010_2012_withvars_021716_mechanisms.dta

capture log close
set more off, perm
set matsize 1000
********************************************************************************

drop if inlist(statefip,2,15)


sort statefip
tempfile a
save `a', replace

keep statefip vet_bplf1yr vet_stlf1yr year
duplicates drop
keep if year==2007
drop year
egen totalvet = sum(vet_stlf1yr)
egen totalvet_bp=sum(vet_bplf1yr)
g vetratio = vet_stlf1yr/totalvet
g vetratio_bp=vet_bplf1yr/totalvet_bp
keep statefip vetratio*
sort statefip
merge 1:m statefip using `a', nogen keep(match)

eststo clear
tsset

g nat_vet_change = ln(predicted_total/L2.predicted_total)
replace nat_vet_change=ln(predicted_total/L3.predicted_total) if nat_vet_change==.

g log_vet_var= nat_vet_change*vetshare_vsoe* vetratio
g log_vet_var_bp =nat_vet_change*vetshare_vsoe* vetratio_bp

summ dvet_supdem_bgt_bp_predic,d
replace dvet_supdem_bgt_bp_predic=. if dvet_supdem_bgt_bp_predic>r(p95) | dvet_supdem_bgt_bp_predic<r(p5)

summ dvet_supdem_bgt_bp_predic,d
replace dvet_supdem_bgt_bp_predic=. if dvet_supdem_bgt_bp_predic>r(p95) | dvet_supdem_bgt_bp_predic<r(p5)


summ dHWOLsupdemratebroad,d
replace dHWOLsupdemratebroad=. if dHWOLsupdemratebroad>r(p95) | dHWOLsupdemratebroad<r(p5)

summ dBGTsupdemratebroad,d
replace dBGTsupdemratebroad=. if dBGTsupdemratebroad>r(p95) | dBGTsupdemratebroad<r(p5)

summ dvet_supdem_bgt,d
replace dvet_supdem_bgt=. if dvet_supdem_bgt>r(p95) | dvet_supdem_bgt<r(p5)




*****************************************************************
/*Table 3: State FE*/

foreach var in $educ $exper {

if "`var'"=="$educ" {
		capture drop avgageexp
		gen avgageexp=bdshare2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		capture drop dubdage
		gen dubdage=dubd
	}

	if "`var'"=="$exper" {
		capture drop avgageexp
		gen avgageexp=avgage2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		capture drop dubdage
		gen dubdage=du35plus
	}

foreach unemp in dHWOLsupdemratebroad dBGTsupdemratebroad  {
eststo clear


	eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' `unemp'  $weight $restric , a(occyear) vce(cluster statefip)
		estadd local occyrfe "Yes", replace
		estadd local statefe "No", replace
		estadd local occstfe "No", replace
		estadd local baseline "Yes", replace
		
	eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' `unemp'  $weight $restric , a(occyear statefip) vce(cluster statefip)
		estadd local occyrfe "Yes", replace
		estadd local statefe "Yes", replace
		estadd local occstfe "No", replace
		estadd local baseline "Yes", replace
		
		eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' `unemp'   $weight $restric , a(occyear panelid) vce(cluster statefip)
		estadd local occyrfe "Yes", replace
		estadd local statefe "No", replace
		estadd local occstfe "Yes", replace
		estadd local baseline "Yes", replace	


}
}

/*
********************************************************************************

/*Table 5: Traded/Non-Traded*/

preserve
	collapse (mean) traded, by(occsoc_minor)
	qui summ traded,d
	local medtrade=r(p50)
	di "`medtrade'"
restore

capture drop dtraded
gen dtraded=traded>`medtrade' & traded!=.

local medtrade 50

foreach var in $educ $exper {

eststo clear

	if "`var'"=="$educ" {
		capture drop avgageexp
		gen avgageexp=bdshare2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		capture drop dubdage
		gen dubdage=dubd
	}

	if "`var'"=="$exper" {
		capture drop avgageexp
		gen avgageexp=avgage2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		capture drop dubdage
		gen dubdage=du35plus
	}

	eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' du $covars  $weight $restric & traded>`medtrade' , a(occyear) vce(cluster statefip)
		estadd local baseline "Yes", replace
		estadd local occyrfe "Yes", replace
	eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' du $covars  $weight $restric & traded<`medtrade'  , a(occyear) vce(cluster statefip)
		estadd local baseline "Yes", replace
		estadd local occyrfe "Yes", replace
	eststo, addscalars(ar3 e(r2_a)): reghdfe d`var' du c.traded#c.du  $covars  $weight $restric  , a(occyear) vce(cluster statefip)
		estadd local baseline "Yes", replace
		estadd local occyrfe "Yes", replace


esttab using $pathUPS/revised_paper/output/temp/traded_`var'.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel  depvars booktabs ///
obslast label scalars("baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects")  wrap nonotes   ///
$tablecoeff $tablenotes6 $sub_in2 ///
keep(du c.traded#c.du) 

esttab using $pathUPS/revised_paper/output/final/traded_`var'.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel fragment nomtitles booktabs ///
obslast label scalars("baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects")  wrap nonotes  interaction(\times)  ///
$tablecoeff $tablenotes6 $sub_in ///
keep(du c.traded#c.du) 

qui eststo dir
local t2b=r(names)
di in red substr("`t2b'",-2,2) + " columns"

}


********************************************************************************

/*Pooled Vet*/


eststo clear

foreach var in $educ $exper {
	foreach vetshock in ldvet_vsoe dvet_supdem_bgt {
		if "`var'"=="$educ" {
			capture drop avgageexp
			gen avgageexp=bdshare2000
			capture drop initialneed
			gen initialneed=initial_`var'07
			capture drop dubdage
			gen dubdage=dubd
		}

		if "`var'"=="$exper" {
			capture drop avgageexp
			gen avgageexp=avgage2000
			capture drop initialneed
			gen initialneed=initial_`var'07
			capture drop dubdage
			gen dubdage=du35plus
		}
		
			eststo: reghdfe d`var' `vetshock' $covars  $weight $restric , a(occyear) vce(cluster statefip)
			estadd local baseline "Yes", replace
			estadd local occfe "Yes", replace
			estadd local statefe "No", replace
		
			if substr("`vetshock'",-5,5)!="total" {
			eststo: reghdfe d`var' `vetshock' $covars  $weight $restric , a(occyear statefip) vce(cluster statefip)
			estadd local baseline "Yes", replace
			estadd local occfe "Yes", replace
			estadd local statefe "Yes", replace
			}
	}	
}	

esttab using $pathUPS/revised_paper/output/final/vet_2.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nomtitles fragment booktabs ///
obslast label scalars("baseline Baseline Controls" "occfe Occ Fixed Effects" "statefe State Fixed Effects") wrap nonotes   ///
$tablecoeff $tablenotes6 $sub_in ///
keep(log* dvet*)

qui eststo dir
local t2b=r(names)
di in red substr("`t2b'",-2,2) + " columns"

*********************************************************************************

/* Vet IV*/

eststo clear

foreach var in $educ $exper {
	foreach vetshock in dvet_supdem_bgt_predic dvet_supdem_bgt_bp_predic  {
		if "`var'"=="$educ" {
			capture drop avgageexp
			gen avgageexp=bdshare2000
			capture drop initialneed
			gen initialneed=initial_`var'07
			capture drop dubdage
			gen dubdage=dubd
		}

		if "`var'"=="$exper" {
			capture drop avgageexp
			gen avgageexp=avgage2000
			capture drop initialneed
			gen initialneed=initial_`var'07
			capture drop dubdage
			gen dubdage=du35plus
		}
		
			eststo, addscalars(AP_Ftest e(rkf)): reghdfe d`var' $covars i.year  (dBGTsupdemratebroad=`vetshock') $weight $restric , a(occsoc_num) vce(cluster statefip)
			estadd local baseline "Yes", replace
			estadd local occfe "Yes", replace
			estadd local statefe "No", replace
		
			if substr("`vetshock'",-5,5)!="total" {
			eststo, addscalars(AP_Ftest e(rkf)): reghdfe d`var' $covars i.year  (dBGTsupdemratebroad=`vetshock') $weight $restric , a(occsoc_num statefip) vce(cluster statefip)
			estadd local baseline "Yes", replace
			estadd local occfe "Yes", replace
			estadd local statefe "Yes", replace
			}
	}	
}	

esttab using $pathUPS/revised_paper/output/final/vet_iv_2.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nomtitles fragment booktabs ///
obslast label scalars("baseline Baseline Controls" "occfe Occ Fixed Effects" "statefe State Fixed Effects") wrap nonotes   ///
$tablecoeff $tablenotes6 $sub_in ///
keep(dBGT*)


*********************************************************************************

/*Mechanisms*/


eststo clear
foreach var in $educ $exper {

	if "`var'"=="$educ" {
		capture drop avgageexp
		gen avgageexp=bdshare2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		}

	if "`var'"=="$exper" {
		capture drop avgageexp
		gen avgageexp=avgage2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		}
	
		foreach mech in replace_rate  otjt hwagepremium_07 union_state_mem_2007 {

		eststo, :reghdfe d`var' c.du##c.`mech'  $covars $weight $restric , a(occyear) vce(cluster statefip)
		estadd local baseline "X", replace
		estadd local occyrfe "X", replace	
		}
		}
	
esttab using $pathUPS/revised_paper/output/temp/mechanisms.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel booktabs ///
obslast depvars label scalars("baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects" ) wrap   ///
$tablecoeff   $sub_in2  keep( c.du* du) 

esttab using $pathUPS/revised_paper/output/final/mechanisms.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nomtitles fragment booktabs ///
obslast depvars label scalars( "baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects") wrap nonotes  ///
$tablecoeff   $sub_in2  keep( c.du* du) 

qui eststo dir
local t2b=r(names)
di in red substr("`t2b'",-2,2) + " columns"



*********************************************************************************

/*Wages*/

eststo clear
 
foreach wage in dhwagepremium  dh_mean  {

foreach var in $educ $exper {

	if "`var'"=="$educ" {
		capture drop avgageexp
		gen avgageexp=bdshare2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		}

	if "`var'"=="$exper" {
		capture drop avgageexp
		gen avgageexp=avgage2000
		capture drop initialneed
		gen initialneed=initial_`var'07
		}
	
		eststo, :reghdfe `wage' d`var'   $covars $weight $restric , a(occyear) vce(cluster statefip)
		estadd local baseline "X", replace
		estadd local occyrfe "X", replace	
		
		eststo, :reghdfe `wage' d`var' du  $covars $weight $restric , a(occyear) vce(cluster statefip)
		estadd local baseline "X", replace
		estadd local occyrfe "X", replace	
		}
		}
		
		
	
esttab using $pathUPS/revised_paper/output/temp/wages.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel booktabs ///
obslast depvars label scalars("baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects" ) wrap   ///
$tablecoeff   $sub_in2  keep(d$educ d$exper du) 

esttab using $pathUPS/revised_paper/output/final/wages.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nomtitles fragment booktabs ///
obslast depvars label scalars( "baseline Baseline Controls" "occyrfe Occ \times Year Fixed Effects") wrap nonotes  ///
$tablecoeff   $sub_in2  keep(d$educ d$exper du) order(d$educ d$exper du)

qui eststo dir
local t2b=r(names)
di in red substr("`t2b'",-2,2) + " columns"





********************************************************************************

/*Table A1: Correlation between LS Measures*/

corr s_unemprate s_unemprate_bd s_unemprate_35plus HWOLsupdemrate_occsoc_broad BGTsupdemrate_occsoc_broad if sumsample==1
corr du dubd du35plus dHWOLsupdemratebroad dBGTsupdemratebroad  if estsample==1 

eststo clear
estpost corr s_unemprate s_unemprate_bd s_unemprate_35plus HWOLsupdemrate_occsoc_broad BGTsupdemrate_occsoc_broad if sumsample==1, matrix
esttab using $pathUPS/revised_paper/output/final/corr_level.tex, replace not unstack  noobs fragment nostar


eststo clear
estpost corr du dubd du35plus dHWOLsupdemratebroad dBGTsupdemratebroad  if estsample==1 , matrix
esttab using $pathUPS/revised_paper/output/final/corr_chg.tex, replace not unstack  noobs fragment nostar

********************************************************************************

/*Table A2: By Time Period*/
local unemp dHWOLsupdemratebroad
eststo clear


foreach var in $educ $exper {

	foreach timeper in 2010 2012 {

		if "`var'"=="$educ" {
			capture drop avgageexp
			gen avgageexp=bdshare2000
			capture drop initialneed
			gen initialneed=initial_`var'07
		}

		if "`var'"=="$exper" {
			capture drop avgageexp
			gen avgageexp=avgage2000
			capture drop initialneed
			gen initialneed=initial_`var'07
		}

		eststo, addscalars(ar2 e(r2_a)): reg `var' dHWOLsupdemratebroad  $occfe $stfe $weight $restric & year==`timeper' , cl(state)
			estadd local occfe "", replace
		eststo, addscalars(ar2 e(r2_a)): reg `var' dBGTsupdemratebroad $occfe $stfe  $weight $restric & year==`timeper' , cl(state)
			estadd local occfe "", replace
		*eststo, addscalars(ar2 e(r2_a)): xi:reg d`var' `unemp' $occfe $stfe $covars $weight $restric & year==`timeper' , cl(state)
		*	estadd local occfe "X", replace
	}

}



esttab using $pathUPS/revised_paper/output/tableA2.csv, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nonumbers nomtitles ///
obslast label scalars("occfe Occ Fixed Effects" "ar2 Adj. R-squared") wrap nonotes   ///
$tablecoeff $tablenotes3 $sub_in ///
keep( dHWOLsupdemratebroad dBGTsupdemratebroad  )

esttab using $pathUPS/revised_paper/output/tableA2.tex, replace star(* 0.10 ** 0.05 *** 0.01) se fragment nolabel nonumbers nomtitles booktabs ///
obslast label scalars("occfe Occ Fixed Effects" "ar2 Adj. R-squared") wrap nonotes   ///
$tablecoeff $tablenotes3 $sub_in ///
keep(`unemp' $covars ) ///
order(`unemp' $covars ) 

********************************************************************************

/*Table A3: CPS stuff*/

use $pathUPS/data/estimation/LI_2007_2010_2012_withvars.dta, clear

duplicates drop du dshare_unemp_gtbd dshare_emp_gtbd, force
drop if year==2007
char year[omit] 2012

eststo clear
	eststo: xi:reg dshare_emp_gtbd du i.year , cl(state)
	eststo: xi:reg dshare_unemp_gtbd du i.year , cl(state) 

esttab using $pathUPS/revised_paper/output/tableA3.tex, replace star(* 0.10 ** 0.05 *** 0.01) se nolabel nomtitles fragment booktabs ///
obslast label wrap nonotes   ///
$tablecoeff $sub_in 




